import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.0
import QtQuick.Controls 2.0
/*
  示例内容是三个矩形，下边两个矩形显示的文本可以拖拽到上边的大矩形中显示
*/
Window {
    id:window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ColumnLayout{
        anchors.fill: parent

        //支持拖入文本的矩形
        Rectangle{
            id:drop_area
            Layout.fillHeight: true
            Layout.fillWidth: true
            color: "#b3d9ff"

            Text {
                id: display_label
                anchors.fill: parent
            }
            DropArea{
                anchors.fill: parent
                keys: ["text/plain"]

                //拖拽进来的文本显示在Text里
                onDropped: {
                    if(drop.hasText){
                        console.log("drop text:",drop.text)
                        display_label.text = drop.text
                    }
                    drop.source
                }
                onEntered: {
                    console.log("onEntered!",drag.source,drag.keys);
                }
            }
        }

        //支持拖出文本的矩形
        RowLayout{
            //左
            Rectangle{
                id: rect
                Layout.fillHeight: true
                Layout.fillWidth: true
                color: "#ffad99"

                Item{
                    id:info
                    anchors.fill: parent
//                    Drag.keys: "red"
                    Drag.active: mouseArea.drag.active
                    Drag.hotSpot.x: 25
                    Drag.hotSpot.y: 25
                    Drag.dragType: Drag.Automatic
                    Drag.supportedActions: Qt.CopyAction
                    Drag.mimeData: {
                        "text/plain": label.text
                    }
                }

                Text {
                    id: label
                    anchors.fill: parent
                    text:"rtsp://1111111"
                }
                MouseArea{
                    id:mouseArea
                    anchors.fill: parent
                    drag.target: info
                    onPressed: rect.grabToImage(function(result) {
                        info.Drag.imageSource = result.url
                    },Qt.size(50, 50))
                }
            }

            //右
            Rectangle{
                id: rect1
                Layout.fillHeight: true
                Layout.fillWidth: true
                color: "#ffad99"

                Item{
                    id:info1
                    anchors.fill: parent
//                    Drag.keys: "red"
                    Drag.active: mouseArea1.drag.active
                    Drag.hotSpot.x: 25
                    Drag.hotSpot.y: 25
                    Drag.dragType: Drag.Automatic
                    Drag.supportedActions: Qt.CopyAction
                    Drag.mimeData: {
                        "text/plain": label1.text
                    }
                }

                Text {
                    id: label1
                    anchors.fill: parent
                    text:"rtsp://2222222222"
                }

                MouseArea{
                    id:mouseArea1
                    anchors.fill: parent
                    drag.target: info1
                    onPressed: rect1.grabToImage(function(result) {
                        info1.Drag.imageSource = result.url
                    },Qt.size(50, 50))
                }
            }
        }

    }

}
